//
//  DKNavigationController.m
//  BS1
//
//  Created by 丁侃 on 16/4/24.
//  Copyright © 2016年 丁侃. All rights reserved.
//

#import "DKNavigationController.h"

@interface DKNavigationController ()<UIGestureRecognizerDelegate>



@end

@implementation DKNavigationController

//统一设置导航栏的背景和标题内容
//在initialize方法,此方法是当本类和子类第一次使用的时候就会调用,会被调用多次

+(void)initialize
{
    
    //获取此类的全局的导航栏,来统一设置
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    //UI_APPEARANCE_SELECTOR凡是方法/属性后面带有就可以通过appearance来统一设置
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    
    [navBar setTitleTextAttributes:@{
                                     
                                     NSFontAttributeName: [UIFont systemFontOfSize:18.0],
                                     NSForegroundColorAttributeName: DKColor(0, 0, 0)
                                     
                                     }];
    
   
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
//    DKLog(@"%@",self.interactivePopGestureRecognizer);
    
    //需要先设置不允许系统的侧滑手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    
    pan.delegate = self;
    
    [self.view addGestureRecognizer:pan];
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    //如果是跟控制器,就不触发手势
    
//    DKLog(@"%zd",self.childViewControllers.count);
    
    //代码的简洁
    /*
    if (self.childViewControllers.count > 1) {
        return YES;
    }else{
        return NO;
    }
     */
    return self.childViewControllers.count > 1;
}


 //统一设置导航栏的返回按钮
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{

    //获取到栈顶控制器,设置栈顶控制器的返回按钮,有高亮显示,随意这里是按钮
    /*
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
    [btn setTitle:@"back" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    */
    
    //需要判断当前控制器是否是根控制器,如果是就不设置返回按钮
//    NSLog(@"%zd",self.childViewControllers.count);
    
    if (self.childViewControllers.count > 0) {
        
        //统一设置返回按钮,
        /*
            viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem dk_itemWithTarget:self ation:@selector(backClick) ImageNameNormal:@"navigationButtonReturn" imageNameHighlighted:@"navigationButtonReturnClick" title:@"back" titleNormalColor:[UIColor blackColor] titleHighlightedColor:[UIColor redColor]];
        */
        
        //当返回按钮和屏幕距离过大,我们需要调整距离
        //通过声明来调整按钮与屏幕的距离呢????
        //我们可以通过给按钮设置内边距,来实现
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [btn setTitle:@"back" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [btn sizeToFit];
        [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
        [btn setBackgroundColor:[UIColor redColor]];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
        
        viewController.hidesBottomBarWhenPushed = YES;
        
    }
    
    //此方法一定要最后调用,否则无法根据跟控制器的判断,来精确的隐藏底部条
    [super pushViewController:viewController animated:animated];
}

-(void)backClick
{
    [self popViewControllerAnimated:YES];
}


@end
